<!--
*@description 
*@fileName 药品列表
*@author wxl
*@date 2025/06/11 15:29:11
!-->
<template>
  <div class="vue-box cdf">
    <div class="search-card">
      <div class="search-body">
        <el-form :inline="true" :model="searchData">
          <in-input name="药品代码" v-model="searchData.code" placeholder="请输入药品代码"/>
          <in-input name="药品名称" v-model="searchData.name" placeholder="请输入药品名称"/>
          <el-form-item>
            <el-button class="btn" type="primary" @click="onSearch">搜索</el-button>
            <el-button type="default" plain icon="el-icon-Refresh" @click="sa.f5CurrView()">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="c-card flex1 table-box">
      <div class="margin-bottom">
        <el-button class="btn" type="primary" :icon="Plus" size="small" @click="add">新增</el-button>
        <el-button class="btn" type="danger" :icon="Delete" size="small" @click="onDelete()">删除</el-button>
      </div>
      <com-table
        rowKey="id"
        :tableData="tableData"
        :tableCfg="tableCfg"
        :pagin="searchData"
        showSelection
        showIndex
        showOperation
        operationWidth="170"
        @handleSelectionChange="handleSelectionChange"
        @handleCurrentChange="handleCurrentChange"
        @handleSizeChange="handleSizeChange"
      >
        <template #operation="{ row }">
          <!-- <el-button type="primary" size="small" text @click="get(row)">查看</el-button> -->
          <!-- <el-button style="margin-left: 0" type="primary" size="small" text @click="update(row)">编辑</el-button> -->
          <el-button style="margin-left: 0" type="danger" size="small" text @click="onDelete(row)">删除</el-button>
        </template>
      </com-table>
    </div>
  </div>
</template>
<script setup name="medicine-list">
import { reactive, onMounted, ref } from 'vue'
import comTable from '@/sa-frame/com/table/table.vue'
import dicts from '@/utils/dict.js'
import { Plus, Delete, CopyDocument } from '@element-plus/icons-vue'
import { useUserStore } from '@/store/user'

const userInfo = useUserStore().userInfo
const searchData = reactive({
  pageindex: 0,
  pagesize: 10,
  fixmedins_code: userInfo.fixmedins_code,
})

const tableCfg = ref([
  {
    label: '药品代码',
    prop: 'med_list_codg',
  },
  {
    label: '药品名称',
    prop: 'ver_name',
  },
  {
    label: '药用部位',
    prop: 'med_part',
  },
  {
    label: '用法用量',
    prop: 'cnvl_used',
  },
  {
    label: '性味归经',
    prop: 'natfla',
  }
])

const tableData = ref([])
// 已选择行
const selectedRows = ref([])

// 组件加载时触发
onMounted(() => {
  f5()
})

// 搜索
function onSearch() {
  searchData.pageindex = 0
  f5()
}

// ------------------ 查找数据 ------------------
const f5 = function () {
  let params = searchData  
  sa.ajax('/api/webPresAdmin/CMDPCatalogueList', params, function (res) {
    tableData.value = res.data // 数据
    searchData.total = Number(res.totalItems)
  })
}
// 增加
const add = function () {
  sa.showModel('新增药品', import('./medicine-edit.vue'))
}
// 修改
const update = function (data) {
  sa.showModel('修改药品', import('./medicine-edit.vue'), { id: data.id })
}
// 详情
const get = function (data) {
  sa.showModel('查看详情', import('./medicine-info.vue'), { id: data.id })
}
// 删除
function onDelete(row) {
  const deleteData = row ? [row] : selectedRows.value
  if (!row && !deleteData.length) {
    sa.alert('请选择要删除的行')
    return
  }
  const ids = deleteData.map((item) => item.id).join(',')
  sa.confirm('确定要删除选中的数据？', function () {
    sa.ajax(
      '/api/webPresAdmin/delCMDPCatalogue',
      {
        id: ids,
      },
      function (res) {
        if (res.code == '1') {
          sa.alert(res.msg)
          f5()
        }
      }
    )
  })
}

// 表格页码切换
function handleCurrentChange(pageindex) {
  searchData.pageindex = pageindex
  f5()
}
// 表格size修改
function handleSizeChange(pagesize) {
  searchData.pagesize = pagesize
  f5()
}
// 表格行选择
function handleSelectionChange(val) {
  console.log('handleSelectionChange', val)
  selectedRows.value = val
}
// 对外开放函数
defineExpose({ f5 })
</script>
<style lang="scss" scoped>
.copy-btn {
  border-left: 1px solid #ccc;
  color: #999;
  font-size: 12px;
  padding-left: 15px;
  margin-left: 15px;
  cursor: pointer;
  border-radius: 0 !important;
  height: 20px;
}
</style>